// @charset "utf-8";

define(function(require, exports, module) {
	var publicMethod = require('../../public/public'),
		clientApi = require('../../public/clientapi');

	var ClientQuote = require('../../public/clientquote');

	function TableModule () {
		this.init.apply(this, arguments);
	}

	TableModule.prototype.init = function (params) {
		this.condition = params.condition;
		this.tableId = params.tableId;

		var htmlDom = '<li data-mname="table" data-table-id="'+ this.tableId +'" data-cond="'+ this.condition +'">' +
					'<div class="module-item">' +
						'<div class="module-header">' +
							'<a href="###" class="icon module-close-btn"></a>' +
							'<p class="module-name" title="'+ this.condition +'">'+ this.condition +'</p>' +
						'</div>' +
						'<div class="module-main">' +
							'<div class="table-box">' +
								'<table class="table-head">' +
									'<thead>' +
										'<tr>' +
											'<td>代码</td>' +
											'<td>名称</td>' +
											'<td>涨幅</td>' +
											'<td>现价</td>' +
										'</tr>' +
									'</thead>' +
								'</table>' +
								'<div class="table-scroll">' +
									'<table>' +
										'<tbody id="tbody'+ this.tableId +'">' +
											// '<tr>' +
											// 	'<td class="txt-l pdl10">恒生市场</td>' +
											// 	'<td class="txt-r pdr30">2369.20</td>' +
											// 	'<td class="txt-r pdr30">23.26</td>' +
											// 	'<td class="txt-r pdr30">0.69%</td>' +
											// '</tr>' +
										'</tbody>' +
									'</table>' +
								'</div>' +
							'</div>' +
						'</div>' +
					'</div>' +
				'</li>';

				$('#modules-list-ul').append(htmlDom);
			this.requestData();
	}
	TableModule.prototype.requestData = function () {
		var self = this;
		var sendParams = {
			uid: clientApi.userId,
			question: self.condition,
			version: clientApi['_version']
		};
		$.support.cors = true;
		publicMethod.ajaxJson({
			type: 'POST',
			url: 'http://sp.10jqka.com.cn/liuyan/autoreply/selectstock/',
			data: sendParams,
			callback: function(result) {
				if (result && result.errorcode === 0) {
					var rstText = result.result;

					if (rstText.indexOf('data-type="xuangu"') != -1) {
						var codeNameArr = $(rstText).attr('data').split(',');
						var codeArr = [];
						var stocksHtml = [];
						$.each(codeNameArr, function(i, val) {
							var dataArr = val.split('-');
							if (i < 40) {
								codeArr.push(dataArr[0]);
								var trHtml = '<tr data-code="' + dataArr[0] + '">' +
												'<td class="txt-c pdl10">' + dataArr[0] + '</td>' +
												'<td class="txt-l pdl10">' + dataArr[1] + '</td>' +
												'<td class="txt-r pdr30">--</td>' +
												'<td class="txt-r pdr30">--</td>' +
											'</tr>';
								stocksHtml.push(trHtml);
							}
						});

						$('#tbody'+ self.tableId).html(stocksHtml.join(''));
						self.requestQuote(codeArr);
					} else {

					}
				} else {

				}
			}
		});
	}
	TableModule.prototype.requestQuote = function (codeArr) {
		var code = codeArr.join(',');
		var quote = 'ZQMC,NEW,ZHANGDIEFU,SETTLE,PRE';
		var reqCallback = function(data) {
			$.each(codeArr, function(i, val) {
				var newprice = '--';
				if (data[val]['NEW'] != 'NUL') {
					newprice = parseFloat(data[val]['NEW']).toFixed(2);
				}
				var zdf = '--';
				if (data[val]['ZHANGDIEFU'] != 'NUL' && data[val]['ZHANGDIEFU']) {
					zdf = parseFloat(data[val]['ZHANGDIEFU']).toFixed(2) + '%';
				}
				$('[data-code="' + val + '"]').each(function() {
					$(this).find('td').eq(2).text(zdf);
					$(this).find('td').eq(3).text(newprice);
				});
			});
		};
		var params = {
			stocks: code,
			quote: quote,
			period: 'now',
			callback: reqCallback,
			// isNeedPush: true
			isNeedPush: false
		};
		var reqQuote = null;
		if (reqQuote) {
			reqQuote.unregisterPush();
		}
		try {
			reqQuote = new ClientQuote(params);
			reqQuote.request();
		} catch (e) {}
	}

	TableModule.prototype.updateSeries = function() {

	}


	module.exports = TableModule;
});